package sctu.edu.java.year2016.team02.project.pc.UI;

import sctu.edu.java.year2016.team02.project.pc.loginMan.Operator;
import sctu.edu.java.year2016.team02.project.pc.problem.PcProblem;
import sctu.edu.java.year2016.team02.project.pc.problem.SocketListener;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Eoly on 2016/6/11.
 */
//管理员登陆界面
public class AdminUI extends JFrame implements ActionListener,ListSelectionListener {
    private Operator op;
    private JList list;
    private PcProblem pb;
    private JRadioButton unSolved;
    private JRadioButton Solved;
    private JRadioButton AllProblem;
    private ButtonGroup group;
    private DefaultListModel model;
    private JScrollPane scroForList;
    private int option = 0;
    private List<Object[]> lt = null;

    public AdminUI(String title,Operator op){
        super(title);
        this.op = op;
        try {
            Init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        setBounds(SocketListener.screenWidth/4, SocketListener.ScreenHeight/4, 600, 400);
        add(AllProblem);
        add(Solved);
        add(unSolved);
        setLayout(null);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void Init() throws SQLException {
        pb = new PcProblem();
        unSolved = new JRadioButton("Un Solved",true);
        Solved = new JRadioButton("Solved");
        AllProblem = new JRadioButton("Show All");
        group = new ButtonGroup();
        group.add(unSolved);group.add(Solved);group.add(AllProblem);
        creatList();
        AllProblem.setBounds(30, 45, 90, 25);
        unSolved.setBounds(130, 45, 90, 25);
        Solved.setBounds(230, 45, 90, 25);
        list.addListSelectionListener(this);
        AllProblem.addActionListener(this);
        unSolved.addActionListener(this);
        Solved.addActionListener(this);
    }

    private void creatList() throws SQLException {
        model = new DefaultListModel();
        list = new JList(model);
        lt = pb.seeking(option);
        Iterator<Object[]> it = lt.iterator();
        while (it.hasNext()) {
            Object[] k=it.next();
            //k[2] 问题描述 k[5] 是否解决
            model.addElement(FormalOf("问题描述："+k[2],18)+FormalOf("是否解决："+PcProblem.isSolved(k[5]),12));
        }
        scroForList =new JScrollPane(list);
        scroForList.setBounds(30, 75, 540, 250);
        add(scroForList);
    }
    private String FormalOf(String str,int forNum){
        str = "　"+str;
        if(str.length()<forNum+1) {
            while (str.length() < forNum+1) {
                str = str + "　";
            }
        }else{
            char k[]=str.toCharArray();
            str = new String(k,0,forNum)+"…";
        }
        return str;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==unSolved||e.getSource()==Solved||e.getSource()==AllProblem) {
            if (e.getSource() == unSolved) {
                option = 0;
            }
            if (e.getSource() == Solved) {
                option = 1;
            }
            if (e.getSource() == AllProblem) {
                option = 2;
            }
            try {
                lt = pb.seeking(option);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            model.removeAllElements();
            Iterator<Object[]> it = lt.iterator();
            while (it.hasNext()) {
                Object[] k = it.next();
                //k[2] 问题描述 k[5] 是否解决
                model.addElement(FormalOf("问题描述：" + k[2], 18) + FormalOf("是否解决：" + PcProblem.isSolved(k[5]), 12));
            }
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if(!list.isSelectionEmpty()) {
            new WatchUI(this, "Watching", lt.get(list.getSelectedIndex()), true, op);
            model.removeAllElements();
            try {
                lt = pb.seeking(option);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            Iterator<Object[]> it = lt.iterator();
            while (it.hasNext()) {
                Object[] k = it.next();
                //k[2] 问题描述 k[5] 是否解决
                model.addElement(FormalOf("问题描述：" + k[2], 18) + FormalOf("是否解决：" + PcProblem.isSolved(k[5]), 12));
            }
        }
    }
}
